package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.statistic.domain.WDayExitWarehouseStat;
import com.ruoyi.statistic.domain.WStatisticDimensionBigit;
import com.ruoyi.statistic.service.IWDayExitWarehouseGoodsRelService;
import com.ruoyi.statistic.service.IWDayExitWarehouseStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author wzy
 */
@Component("dxTask")
public class DxTask
{
    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWDayExitWarehouseStatService wDayExitWarehouseStatService;

    @Autowired
    private IWDayExitWarehouseGoodsRelService wDayExitWarehouseGoodsRelService;

    private static Logger logger = LoggerFactory.getLogger(DxTask.class);

    /**
     * 统计日出库量，无参，按出库客户，货物类型，维度统计
     */

    public void statisticDayExitWarehouseTotal(String strExitWarehouseDate)
    {
        logger.debug("统计，日出库量，出库客户，货物类型，开始 ...");
        /**
         * 计算当前日期，前一天日期
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        if(StringUtils.isNotEmpty(strExitWarehouseDate)) {
            today = LocalDate.parse(strExitWarehouseDate, formatter);
        }
        logger.debug("当前日期: " + today);

        LocalDateTime time = LocalDateTime.now();
        logger.debug("当前时间: " + time);
        //LocalDateTime today8 = LocalDateTime.of(time.toLocalDate(), LocalTime.MIN.withHour(8));
        LocalDateTime today8 = LocalDateTime.of(time.toLocalDate(), LocalTime.of(8, 0, 0));
        logger.debug("当前8点: " + today8);
        LocalDateTime yesterday8 = today8.minusDays(1);
        logger.debug("前天8点: " + yesterday8);

        // 获取前一天的日期
        LocalDate yesterday = today.minusDays(1);
        logger.debug("前一天日期: " + yesterday);

        TGoodsInfo tGoodsInfo = new TGoodsInfo();

        Double exitWarehouseWeightTotal = 0.0, exitWarehouseTheoryWeightTotal = 0.0;

        WDayExitWarehouseStat wDayExitWarehouseStat = new WDayExitWarehouseStat();

        String dayExitStatisticNumber;

        Long dayExitStatisticId = 0L;

        List<Long> goodsIds = new ArrayList<>();
        /**
         * 按出库日期，筛选出库客户，货物类型，列表
         */
        //List<WStatisticDimensionBigit> statisticDimensionBigits = tGoodsInfoService.selectStatisticDimensionByExitWarehouseDate(Date.valueOf(yesterday), Date.valueOf(yesterday));
        List<WStatisticDimensionBigit> statisticDimensionBigits = tGoodsInfoService.selectStatisticDimensionByExitWarehouseDate(DateUtils.toDate(yesterday8), DateUtils.toDate(today8));
        //logger.debug("statisticDimensionBigits: " + statisticDimensionBigits);
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof Map) );
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof List) );

        /**
         * 遍历合约号，货物
         */
        for(WStatisticDimensionBigit statisticDimensionBigit : statisticDimensionBigits)
        {
            if(StringUtils.isNull(statisticDimensionBigit)) continue;

            /**
             * 按出库客户，货物类型。出库日期，查询货物
             */
            tGoodsInfo.setExitWarehouseCustomer(statisticDimensionBigit.getCustomer());
            tGoodsInfo.setGoodsType(statisticDimensionBigit.getGoodsType());
            tGoodsInfo.setWarehouseLocateName(statisticDimensionBigit.getWarehouseLocateName());
            //logger.debug("OOO " + "statisticDayExitWarehouseTotal = " + tGoodsInfo);

            //List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByExitStatistic(tGoodsInfo, Date.valueOf(yesterday), Date.valueOf(yesterday));
            List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByExitStatistic(tGoodsInfo, DateUtils.toDate(yesterday8), DateUtils.toDate(today8));
            /**
             * 合计出库货物重量，记录货物ID
             */
            for(TGoodsInfo good : goods)
            {
                exitWarehouseWeightTotal = exitWarehouseWeightTotal + good.getWeight();
                exitWarehouseTheoryWeightTotal = exitWarehouseTheoryWeightTotal + good.getTheoryWeight();
                goodsIds.add(good.getGoodsId());
            }

            /**
             * 保护，goodsIds，为空，直接退出，本次循环
             */
            if(goodsIds.isEmpty()) break;

            /**
             * 日出库量编号
             */
            dayExitStatisticNumber = OrderUtils.getOrderNumber("DX", "-");

            /**
             * 出库重量，四舍五入
             */
            exitWarehouseWeightTotal = Math.round(exitWarehouseWeightTotal * 1000.0) / 1000.0;
            exitWarehouseTheoryWeightTotal = Math.round(exitWarehouseTheoryWeightTotal * 1000.0) / 1000.0;

            /**
             * 日出库量，数据准备
             */
            wDayExitWarehouseStat.setDayExitStatisticNumber(dayExitStatisticNumber);
            wDayExitWarehouseStat.setExitWarehouseDate(DateUtils.toDate(yesterday));
            wDayExitWarehouseStat.setExitWarehouseCustomer(statisticDimensionBigit.getCustomer());
            wDayExitWarehouseStat.setGoodsType(statisticDimensionBigit.getGoodsType());
            wDayExitWarehouseStat.setWeightMode(statisticDimensionBigit.getWeightMode());
            wDayExitWarehouseStat.setDayExitWarehouseWeight(exitWarehouseWeightTotal);
            wDayExitWarehouseStat.setDayExitWarehouseTheoryWeight(exitWarehouseTheoryWeightTotal);
            wDayExitWarehouseStat.setCreateBy("DxTask");
            wDayExitWarehouseStat.setCreateTime(DateUtils.getNowDate());

            /**
             * 日出库量，插入数据
             */
            wDayExitWarehouseStatService.insertWDayExitWarehouseStat(wDayExitWarehouseStat);
            //sleep(300);
            /**
             * 获取插入记录，ID
             */
            dayExitStatisticId = wDayExitWarehouseStat.getDayExitStatisticId();
            //logger.debug("OOO " + "dayExitStatisticId = " + dayExitStatisticId);

            /**
             * exitWarehouseWeightTotal清零
             */
            exitWarehouseWeightTotal = 0.0;
            exitWarehouseTheoryWeightTotal = 0.0;

            /**
             * 入出库量关联表，数据插入
             */
            //logger.debug("OOO " + "goodsIds = " + goodsIds);
            wDayExitWarehouseGoodsRelService.insertWDayExitWarehouseGoodsRelExt(dayExitStatisticId, goodsIds);
            goodsIds.clear();

        }

        logger.debug("统计，日出库量，出库客户，货物类型，结束 ...");
    }

}
